package com.xk857.system.mapper.provider;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import java.util.List;

/**
 * @author CV大魔王
 * @Description 角色相关动态SQL
 * @date 2021/2/25 21:40
 */
public class SysUserProvider {

    /**
     * 新增用户角色关系数据到 sys_user_role
     *
     * @param userId  用户id
     * @param roleIds 角色ids集合
     * @return sql语句
     */
    public String saveUserRole(String userId, List<String> roleIds) {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO sys_user_role(id, user_id, role_id) VALUES ");
        for (int i = 0; i < roleIds.size(); i++) {
            if (i == roleIds.size() - 1) {
                sql.append(" ( ").append(IdWorker.getId()).append(" , ").append(userId).append(" , ").append(roleIds.get(i)).append(" ) ");
            } else {
                sql.append(" ( ").append(IdWorker.getId()).append(" , ").append(userId).append(" , ").append(roleIds.get(i)).append(" ), ");
            }
        }
        return sql.toString();
    }

}
